﻿using CsharpHttpHelper;
using CsharpHttpHelper.Enum;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Net.WebRequestMethods;

namespace 导入工具
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
        }

        string url;
        Thread thread;
        private void button1_Click(object sender, EventArgs e)
        {

        
            using (FolderBrowserDialog fbd = new FolderBrowserDialog())
            {
                // 显示对话框
                DialogResult result = fbd.ShowDialog();
                if (result == DialogResult.OK)
                {
                    // 获取选择的目录路径
                    string selectedFolderPath = fbd.SelectedPath;
                 textBox1.Text = selectedFolderPath;
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            url = textBox2.Text;
            string folderPath = textBox1.Text;
            if (string.IsNullOrEmpty(folderPath))
            {
                MessageBox.Show("未设置目录");
                return;
            }
            thread = new Thread(MainW);
            thread.Start();
        }

        private void MainW()
        {
            string folderPath = textBox1.Text;
            string newImagepath = folderPath + "\\手动传图片\\";
            string[] dirs = Directory.GetDirectories(folderPath);
            int count = 0;
            foreach (string dir in dirs)
            {
                string muluname = getDirName(folderPath, dir);

                string[] files = Directory.GetFiles(dir);

                for (int index = 0; index < files.Length; index++)
                {
                    files[index] = getDirName(dir, files[index]);
                }

                string restr = CreatCase(muluname, files);
                if (string.IsNullOrEmpty(restr))
                {
                    continue;
                }
                count++;
                //修改目录名字，
                string newpath = folderPath + "\\" + restr;
                Directory.Move(dir, newpath);

                if (!Directory.Exists(newImagepath))
                {
                    Directory.CreateDirectory(newImagepath);
                }
                //把图片复制到一个目录
                foreach (string file in files)
                {
                    string oldf = newpath + "\\" + file;
                    string newf = newImagepath + restr +"_"+ file;
                    System.IO.File.Copy(oldf, newf, true);
                }

            }
            richTextBox1.AppendText("完成:"+ count + "\r\n");
           
        }

        private string getDirName(string path, string fuulnanme)
        {
            string muluname = fuulnanme.Replace(path, "");
            muluname = muluname.Substring(1);
            return muluname;
        }


        private string CreatCase(string dirname, string[] filename)
        {
           
            HttpHelper httpHelper = new HttpHelper();



            var postdata = new
            {
                dirName = HttpHelper.URLEncode(dirname, Encoding.UTF8),
                files = string.Join("|", filename)
            };
           HttpItem httpItem = new  HttpItem()
            {

                URL = url + "/api/car_case/addfromform",
                Postdata = Newtonsoft.Json.JsonConvert.SerializeObject(postdata),
                Method = "POST",
               UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:18.0) Gecko/20100101 Firefox/18.0",
               Accept = "text/html, application/xhtml+xml, */*",//   
               ContentType = "application/json; charset=utf-8",
                Timeout = 100000,
                Allowautoredirect = true,
               ReadWriteTimeout = 30000,
               IsToLower = false,
             
               AutoRedirectCookie = false,
               ResultType = ResultType.String,
              // SecurityProtocol = SecurityProtocolType.Ssl3,
               // CerPath= "D:\\GitDoc_MyGitee\\hxh-color-git\\Admin.NET\\导入工具\\123.cer"

           };
      //    httpItem.ClentCertificates = new X509CertificateCollection();
     //   httpItem.ClentCertificates.Add(new X509Certificate("C:\\Users\\10467\\Downloads\\1\\IIS\\hxhcarcolorapi.yokfilm.com_ssl.cer", "785801"));
        //    httpItem.ClentCertificates.Add(new X509Certificate("C:\\Users\\10467\\Downloads\\1\\hxhcarcolorapi.yokfilm.com_ssl.cer", "785801"));
            // httpItem.Header.Add("Content-Length", httpItem.Postdata.Length.ToString());

            HttpResult res= httpHelper.GetHtml(httpItem);
           
            if (Regex.IsMatch(res.Html, "\"result\":(\\d+),"))
            {
                string id = Regex.Match(res.Html, "\"result\":(\\d+),").Groups[1].Value;
                richTextBox1.AppendText(dirname + "：成功导入ID：" + id + "\r\n");
                return id;
            }
            else
            {
                richTextBox1.AppendText(dirname+"：失败："+res.Html+ "\r\n");
                return "";
            }
          
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
